unit clsInstrucao;

interface

uses Dm,dbtables,biblio;

type
  TInstrucao=Class(TObject)
    private
      Inst_Codigo   :String;
    public
      Inst_descricao:string;
      function  Gravar(p_Inst_Codigo:String):Byte;
      function  Recuperar(p_Inst_Codigo:String):Byte;
      function  Excluir(p_Inst_Codigo:String):Byte;
      procedure Consultar(var p_q:TQuery; p_descricao:String);
  end;

implementation

function TInstrucao.Gravar;
var tq:TQuery;
begin
  tq:=Tquery.Create(DmInt);
  tq.databasename:=dmint.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Instrucao where Inst_Codigo=:k_Inst_Codigo');
  tq.parambyname('k_Inst_Codigo').asstring:=strzero(p_Inst_Codigo,3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('INSERT INTO Instrucao VALUES(:k_Inst_Codigo,:k_Inst_descricao)');
  end else begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('UPDATE Instrucao SET Inst_descricao=:k_Inst_descricao WHERE Inst_Codigo=:k_Inst_Codigo');
  end;
  tq.parambyname('k_Inst_Codigo').asstring:=strzero(p_Inst_Codigo,3);
  tq.parambyname('k_Inst_descricao').asstring:=Inst_descricao;
  tq.close;
  tq.execsql;
  result:=1;
  tq.close;
  tq.Free;
end;

function TInstrucao.Recuperar;
var tq:TQuery;
begin
  tq:=Tquery.Create(DmInt);
  tq.databasename:=dmint.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Instrucao where Inst_Codigo=:k_Inst_Codigo');
  tq.Params[0].asstring:=strzero(p_Inst_Codigo,3);
  tq.open;
  Inst_Codigo :=p_Inst_Codigo;
  if tq.eof then begin
    Inst_descricao :='';
    Result:=0;
  end else begin
    Inst_descricao:=tq.fieldbyname('Inst_descricao').asstring;
    Result:=1;
  end;
  tq.Close;
  tq.Free;
end;

function TInstrucao.Excluir;
var tq:TQuery;
begin
  tq:=Tquery.Create(DmInt);
  tq.databasename:=dmint.Db.DatabaseName;
  tq.active:=False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Instrucao WHERE Inst_Codigo=:k_Inst_Codigo');
  tq.parambyname('k_Inst_Codigo').asstring:=strzero(p_Inst_Codigo,3);
  tq.execSQL;
  Result:=1;
  tq.close;
  tq.Free;
end;

procedure TInstrucao.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Instrucao where Inst_descricao>=:k_Inst_descricao order by Inst_descricao');
  p_q.parambyname('k_Inst_descricao').asstring:=p_descricao;
end;

end.
